<?php
// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\widget;


use app\admin\model\Item;
use app\admin\model\LayoutDesc;
use think\facade\View;
use think\template\TagLib;

/**
 * 布局标签组件
 * @author 牧羊人
 * @since 2020/7/2
 * Class Layout
 * @package app\admin\taglib
 */
class Layout extends TagLib
{
    // 自定义标签列表
    protected $tags = [
        'select' => ['attr' => 'name', 'close' => 0],
    ];

    /**
     * 布局组件
     * @param $tag 标签参数
     * @return string
     * @author 牧羊人
     * @since 2020/7/2
     */
    public function tagSelect($tag)
    {
        // 站点ID
        $itemId = $tag['itemId'];
        // 位置编号
        $locId = $tag['locId'];
        // 层级
        $limit = isset($tag['limit']) ? intval($tag['limit']) : 1;
        $layoutList = [
            1 => [
                'tname' => '站点',
                'code' => 'page',
            ],
            2 => [
                'tname' => '页面位置',
                'code' => 'loc',
            ],
        ];

        $layoutDescModel = new LayoutDesc();
        $result = $layoutDescModel->where(['item_id' => $itemId, 'mark' => 1])->column('id,item_id,loc_id,loc_desc');
        $item = array();
        if (is_array($result)) {
            foreach ($result as $k => $v) {
                $item[$v['loc_id']]['id'] = $v['loc_id'];
                $item[$v['loc_id']]['name'] = $v['loc_desc'];
            }
        }

        //获取站点
        $itemModel = new Item();
        $item_list = $itemModel->where(['status' => 1, 'mark' => 1])->column('id,name');
        $layoutList[1]['list'] = $item_list;
        $layoutList[1]['selected'] = $itemId;
        $layoutList[2]['list'] = $item;
        $layoutList[2]['selected'] = $locId;
        $layoutList = array_slice($layoutList, 0, $limit);
        // 绑定数据
        View::assign("layoutList", $layoutList);
        // 模板渲染
        return View::fetch("widget/layout/layout_select");
    }
}